package com.jcfk.system.service;

import com.jcfk.system.domain.po.SysPortalContent;
import com.jcfk.system.domain.po.SysPortalContentCatalog;
import com.jcfk.system.domain.query.SysPortalContentQuery;
import com.jcfk.system.domain.dto.SysPortalContentDTO;
import com.jcfk.system.dao.SysPortalContentDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 门户内容
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-03-25
 */
@Service
public class SysPortalContentService extends BaseService<SysPortalContentDao, SysPortalContent> {

    @Autowired
    private SysPortalContentCatalogService sysPortalContentCatalogService;

    /**
     * SysPortalContent分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<SysPortalContentDTO> queryByPage(DataPage<SysPortalContentDTO> page, SysPortalContentQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 插入门户内容
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysPortalContentDTO dto) {

        // 保存
        SysPortalContent item = new SysPortalContent();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新门户内容
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SysPortalContentDTO dto) {

        // 保存
        SysPortalContent item = new SysPortalContent();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 是否已被禁用（0 - 禁用， 1 - 启用）
     */
    public void enabled(SysPortalContent sysPortalContent) {
        SysPortalContent enable = this.getById(sysPortalContent.getId());
        enable.setEnabled(sysPortalContent.getEnabled());
        this.setDefaultValue(enable);
        baseMapper.updateById(enable);
    }

    /**
     * 根据批量主键删除门户内容(逻辑删除)
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(SysPortalContentDTO dto) {

        SysPortalContent item = new SysPortalContent();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<SysPortalContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     */
    public void setDefaultValue(SysPortalContent item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
        item.setDeleted(0);

    }

    /**
     * 根据主键获取门户内容信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public SysPortalContentDTO get(String id) {
        // 查询数据
        SysPortalContent item = this.baseMapper.selectById(id);

        SysPortalContentDTO dto = new SysPortalContentDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }
        // 添加目录名称字段
        SysPortalContentCatalog catalog = sysPortalContentCatalogService.getById(item.getCatalogId());
        dto.setCatalogName(catalog.getCatalogName());

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SysPortalContentDTO>
     */
    public List<SysPortalContentDTO> getList(SysPortalContentQuery query) {
        // 查询条件
        QueryWrapper<SysPortalContent> qw = new QueryWrapper<>();

        qw.lambda().likeRight(!StrUtil.isEmpty(query.getCatalogId()), SysPortalContent::getCatalogId, query.getCatalogId());
        // 查询数据
        List<SysPortalContent> list = super.list(qw);

        return convertList(list, SysPortalContentDTO.class);
    }




}

